/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frame;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import library.DB;

/**
 *
 * @author Thien
 */
public class AdminModel {
    private int ID;
    private String Name;
    private String Pass;

    public AdminModel() {
        this.ID = 0;
        this.Name = "";
        this.Pass = "";
    }

    public AdminModel(String Name, String Pass) {
        this.Name = Name;
        this.Pass = Pass;
        this.ID = 0;
    }

    public AdminModel(int ID, String Name, String Pass) {
        this.ID = ID;
        this.Name = Name;
        this.Pass = Pass;
    }

    public int getID() {
        return ID;
    }

    public String getName() {
        return Name;
    }

    public String getPass() {
        return Pass;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public void setName(String Name) {
        this.Name = Name;
    }

    public void setPass(String Pass) {
        this.Pass = Pass;
    }

    public int save() {
        int result = 0;
        PreparedStatement sm;
        if (this.ID > 0) {
            try {
                sm = DB.getConnection().prepareStatement("Update Admins SET Username = ?, Password = ? WHERE ID = ?");
                sm.setString(1, Name);
                sm.setString(2, Pass);
                sm.setInt(3, ID);
                result = sm.executeUpdate();
            }
            catch (SQLException ex) {
                Logger.getLogger(AdminModel.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        else {
            try {
                sm = DB.getConnection().prepareStatement("INSERT INTO Admins (Username, Password) VALUES (?, ?)");
                sm.setString(1, Name);
                sm.setString(2, Pass);
                result = sm.executeUpdate();
                sm.close();
            }
            catch (SQLException ex) {
                Logger.getLogger(AdminModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return result;
    }

    public static boolean removeAdmin(int ID) {
        boolean result = false;
        if (ID > 0) {
            try {
                PreparedStatement sm = DB.getConnection().prepareStatement("DELETE FROM Admins WHERE ID = ?");
                sm.setInt(1, ID);
                result = sm.executeUpdate() > 0;
                sm.close();
            }
            catch (SQLException ex) {
                Logger.getLogger(AdminModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return result;
    }
}
